package cn.coderdream.mtoast;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.annotation.ColorInt;
import androidx.annotation.StringRes;

public class MToastUtils {
    @SuppressLint("StaticFieldLeak")
    private static MToastUtils mToastUtils;
    @SuppressLint("StaticFieldLeak")
    private static Context mContext;

    private static Toast currentToast;

    public static MToastUtils getInstance() {
        if (mToastUtils == null) {
            mToastUtils = new MToastUtils();
        }
        return mToastUtils;
    }

    public class with {
        private int iconID = 0;
        private int background = 0;
        @StringRes
        private int contentID = 0;
        private String content = "";
        @ColorInt
        private int contentColor = 0;
        private int duration = 0;

        private int gravity, xOffset, yOffset = 0;

        public with(Context context) {
            mContext = context;
            if (null == currentToast) {
                currentToast = new Toast(mContext);
            }
        }

        public with icon(int iconID) {
            this.iconID = iconID;
            return this;
        }

        public with content(String content) {
            this.content = content;
            return this;
        }

        public with content(String content, @ColorInt int contentColor) {
            this.content = content;
            this.contentColor = contentColor;
            return this;
        }

        public with contentID(int contentID) {
            this.contentID = contentID;
            return this;
        }

        public with contentID(int contentID, @ColorInt int contentColor) {
            this.contentID = contentID;
            this.contentColor = contentColor;
            return this;
        }

        public with background(int background) {
            this.background = background;
            return this;
        }

        public with duration(int duration) {
            if (duration >= 1) {
                duration = 1;
            } else {
                duration = 0;
            }
            this.duration = duration;
            return this;
        }

        public with gravity(int gravity, int xOffset, int yOffset) {
            this.gravity = gravity;
            this.xOffset = xOffset;
            this.yOffset = yOffset;
            return this;
        }

        public class show {
            private int showIconID;
            private int showBackgroundSrc;
            private String showContent;
            @ColorInt
            private int showContentColor;
            private int showGravity, showXOffset, showYOffset = 0;

            public show() {
                if (iconID != 0) {
                    showIconID = iconID;
                }
                if (background != 0) {
                    showBackgroundSrc = background;
                } else {
                    showBackgroundSrc = R.drawable.toast_default_shape;
                }
                if (contentColor != 0) {
                    showContentColor = contentColor;
                } else {
                    showContentColor = Color.parseColor("#ffffff");
                }
                if (TextUtils.isEmpty(content) && contentID == 0) {
                    showContent = "";
                } else if (TextUtils.isEmpty(content) && contentID != 0) {
                    showContent = mContext.getResources().getString(contentID);
                } else {
                    showContent = content;
                }
                if (xOffset != 0) {
                    showXOffset = xOffset;
                }
                if (yOffset != 0) {
                    showYOffset = yOffset;
                }
                if (gravity != 0) {
                    showGravity = gravity;
                    currentToast.setGravity(showGravity, showXOffset, showYOffset);
                }
                ToastView toastView = new ToastView(mContext);
                if (!TextUtils.isEmpty(showContent)) {
                    toastView.setContent(showContent);
                }
                if (showBackgroundSrc != 0) {
                    toastView.setBackgroundResources(showBackgroundSrc);
                }
                if (showIconID != 0) {
                    toastView.setIcon(showIconID);
                }
                toastView.setContent_color(showContentColor);
                currentToast.setView(toastView);
                currentToast.setDuration(duration);
                currentToast.show();
            }
        }
    }
}
